package com.example.demo.service;


import com.example.demo.dao.StoreMapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.Store;
import com.example.demo.pojo.BSResult;
import com.example.demo.utils.BSResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class StoreServiceImpl implements  StoreService {


    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Store> findAllStores() {
        return storeMapper.selectAll();
    }

    @Override
    public Store findStoreByUserId(Integer userId) {

        Example example=new Example(Store.class);

        Example.Criteria criteria=example.createCriteria();

        criteria.andEqualTo("storeManagerId",userId);

        return storeMapper.selectOneByExample(example);

    }

    @Override
    public Store findStoreById(Integer id) {
        return storeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addStore(Store store) {
        store.setStoreId(null);
        store.setCreated(new Date());
        store.setUpdated(new Date());
        return storeMapper.insert(store);
    }


    @Override
    public int delStoreById(Integer storeId) {
        return storeMapper.deleteByPrimaryKey(storeId);
    }

    @Override
    public BSResult updateStore(Store store) {

       // String username=userMapper.selectByPrimaryKey(store.getStoreManagerId()).getUsername();
       // store.setStoreManagerName(username);
        store.setUpdated(new Date());
        storeMapper.updateByPrimaryKeySelective(store);

        return BSResultUtils.build(200,"修改成功");
    }
}
